﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Faster.Entity;
using Faster.GameBLL;
using System.Threading;

namespace Faster.WinPvZ
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region Event
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("PVZ Tools 1.0");
        }


        #endregion

        #region Field
        private List<Thread> _listThread = null;
        private bool _isLogout = false;
        #endregion

        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            this.webBrowserGame.Navigate(this.toolStripComboBoxAddress.Text);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.webBrowserGame.Navigate("about:blank");
        }

        private void webBrowserGame_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!_isLogout && this.webBrowserGame.DocumentText.IndexOf("我的首页") > -1 && this.webBrowserGame.Url.AbsoluteUri != "http://s1.kaixin001pvz.youkia.com/pvz/index.php/default/main")
            {
                this.webBrowserGame.Navigate("http://s1.kaixin001pvz.youkia.com/pvz/index.php/default/main");
            }
        }

        private void toolStripButtonOpenGamePage_Click(object sender, EventArgs e)
        {
            this.webBrowserGame.Navigate("http://www.kaixin001.com/!app_pvzonline/");
            _isLogout = false;
        }

        private void toolStripButtonLoginTool_Click(object sender, EventArgs e)
        {
            string cookieStr = webBrowserGame.Document.Cookie;
            this.tabLoginGame.SelectedIndex = 1;
            UserInfo info = new UserInfo();
            info.Cookies = cookieStr;
            UserInfoHelper helper = new UserInfoHelper();
            helper.User = info;
            helper.GetUserInfo();

            PlantHelper phelper = new PlantHelper();
            phelper.User = info;
            phelper.GetAllPlant();

            this.listBoxUserList.Items.Add(info);
            
        }

        private void webBrowserGame_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
           
        }

        private void webBrowserGame_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.toolStripComboBoxAddress.Text = this.webBrowserGame.Url.AbsoluteUri;
        }

        private void btnStartWork_Click(object sender, EventArgs e)
        {
            if (_listThread == null)
            {
                _listThread = new List<Thread>();
            }
            
            if (this.btnStartWork.Text == "运行")
            {
                _listThread.Clear();
                this.btnStartWork.Text = "停止";
                foreach (UserInfo info in this.listBoxUserList.Items)
                {
                    Thread thread = new Thread(new ParameterizedThreadStart(Working));
                    thread.Start(info);
                    _listThread.Add(thread);
                }
            }
            else
            {
                this.btnStartWork.Text = "运行";
                foreach (Thread thread in _listThread)
                {
                    
                    thread.Abort();
                }
            }
        }

        public void Working(object obj)
        {
            UserInfo info = obj as UserInfo;
            UserInfoHelper helper = new UserInfoHelper();
            helper.User = info;
            while (true)
            {
                if (Thread.CurrentThread.ThreadState == ThreadState.Stopped)
                {
                    return;
                }
                helper.Watering(info.UserID);
                Thread.Sleep(300);
                helper.Fertilize(info.UserID);
                Thread.Sleep(300);
                helper.Gain(info.UserID);
                Thread.Sleep(500);

                foreach (FriendInfo friend in info.Friends)
                {
                    helper.Watering(friend.FriendID);
                    Thread.Sleep(300);
                    helper.Fertilize(friend.FriendID);
                    Thread.Sleep(300);
                    helper.Gain(friend.FriendID);
                    Thread.Sleep(500);
                }

                Thread.Sleep(1*60000);
            }
        }

        private void toolStripButtonLogout_Click(object sender, EventArgs e)
        {
            _isLogout = true;
            this.webBrowserGame.Navigate("http://www.kaixin001.com/!app_pvzonline/");
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if (this.listBoxUserList.SelectedItem == null)
            {
                MessageBox.Show("请先选择一个用户！");
                return;
            }
            frmSetting set = new frmSetting();
            set.User = this.listBoxUserList.SelectedItem as UserInfo;
            set.ShowDialog(this);
        }
    }
}
